package cn.snmp.mutual.uk.stack;

import cn.snmp.mutual.uk.util.SnmpUtilities;

import java.io.*;

class AsnDecoderv1 extends AsnDecoderBase {
    private static final String version_id = "@(#)$Id: AsnDecoderv1.java,v 3.3 2006/02/09 14:16:36 birgit Exp $ Copyright Westhawk Ltd";

    AsnSequence DecodeSNMP(final InputStream in, final String community) throws IOException, DecodingException {
        final AsnSequence asnTopSeq = this.getAsnSequence(in);
        final int snmpVersion = this.getSNMPVersion(asnTopSeq);
        if (snmpVersion != 0) {
            final String str = SnmpUtilities.getSnmpVersionString(snmpVersion);
            final String msg = "Wrong SNMP version: expected SNMPv1, received " + str;
            throw new DecodingException(msg);
        }
        final String comm = this.getCommunity(asnTopSeq);
        if (!comm.equals(community)) {
            final String msg = "Wrong community: expected " + community + ", received " + comm;
            throw new DecodingException(msg);
        }
        AsnSequence seqPdu = null;
        seqPdu = (AsnPduSequence) asnTopSeq.findPdu();
        if (seqPdu == null) {
            seqPdu = (AsnTrapPduv1Sequence) asnTopSeq.findTrapPduv1();
        }
        return seqPdu;
    }
}
